package com.lgmshare.k3.ui.upgrade;

import android.app.DialogFragment;
import android.os.Bundle;

import androidx.annotation.Nullable;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.lgmshare.k3.R;


/**
 * @author lim
 * @description: TODO
 * @email lgmshare@gmail.com
 * @datetime 2016/7/13 14:30
 */
public class UpgradeDialogFragment extends DialogFragment implements View.OnClickListener {

    private UpdateListener mUpdateListener;

    private String update_version;
    private String update_download;
    private String update_remark;
    private boolean update_force;

    public static UpgradeDialogFragment newInstance(String update_version, String update_download, String update_remark, boolean update_force) {
        UpgradeDialogFragment f = new UpgradeDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("update_version", update_version);
        bundle.putString("update_download", update_download);
        bundle.putString("update_remark", update_remark);
        bundle.putBoolean("update_force", update_force);
        f.setArguments(bundle);
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        update_version = getArguments().getString("update_version");
        update_download = getArguments().getString("update_download");
        update_remark = getArguments().getString("update_remark");
        update_force = getArguments().getBoolean("update_force");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题
        getDialog().setCancelable(false);
        getDialog().setCanceledOnTouchOutside(false);

        View view = inflater.inflate(R.layout.dialog_update, container);
        TextView tv_dialog_title = view.findViewById(R.id.tv_dialog_title);
        TextView tv_dialog_message = view.findViewById(R.id.tv_dialog_message);

        Button btn_dialog_negative = view.findViewById(R.id.btn_dialog_negative);
        View dialog_line = view.findViewById(R.id.dialog_line);
        Button btn_dialog_positive = view.findViewById(R.id.btn_dialog_positive);

        if (update_force) {
            dialog_line.setVisibility(View.GONE);
            btn_dialog_negative.setVisibility(View.GONE);
            btn_dialog_positive.setBackgroundResource(R.drawable.dialog_btn_single_bg);
        }

        tv_dialog_title.setText("版本更新:" + update_version);
        tv_dialog_message.setText(update_remark);
        view.findViewById(R.id.btn_dialog_negative).setOnClickListener(this);
        view.findViewById(R.id.btn_dialog_positive).setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.btn_dialog_negative) {
            if (mUpdateListener != null) {
                mUpdateListener.onCancel();
            }
        } else if (i == R.id.btn_dialog_positive) {
            if (mUpdateListener != null) {
                mUpdateListener.onUpdate();
            }
        }
    }

    public void setUpdateListener(UpdateListener updateListener) {
        mUpdateListener = updateListener;
    }

    public interface UpdateListener {
        void onUpdate();

        void onCancel();
    }
}
